<?php
/**
 * @package Component Metromap for Joomla! 1.5
 * @version $Id: view.html.php 4 2010-08-17 13:58:48Z batyrkin $
 * @author Batyrkin Anton
 * @copyright (C) 2010- Batyrkin Anton
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 **/

defined( '_JEXEC' ) or die();
jimport( 'joomla.application.component.view' );

class MetromapViewMetromap extends JView
{
	function display($tpl = null)
	{
		global $mainframe;

		$tmpl = array();
		$lists 	= array();

		$db		 =& JFactory::getDBO();
		$uri 	 =& JFactory::getURI();
		$user 	 =& JFactory::getUser();
		$editor  =& JFactory::getEditor();
		$document=& JFactory::getDocument();
		$params	 =& $mainframe->getParams();
		$model	 =& $this->getModel();

		$tmpl['page_title']			= $params->get( 'page_title', '' );
		$tmpl['work_mode']			= $params->get( 'work_mode', '1' );
		$tmpl['show_page_title']	= $params->get( 'show_page_title', 1 );
		$tmpl['show_name']			= $params->get( 'show_name', 1 );
		$tmpl['show_description']	= $params->get( 'show_description', 1 );
		$tmpl['show_phone']			= $params->get( 'show_phone', 1 );
		$tmpl['show_address']		= $params->get( 'show_address', 1 );
		$tmpl['show_link']			= $params->get( 'show_link', 1 );

		$sityl = & $this->get('ExistSityList');
		$defsity = array('value'=>(string)$model->_defsityid,'text'=>$model->_defsityname);

		if (!in_array($defsity, $sityl)) {
			array_unshift($sityl,$defsity);
		}

		// build list of categories (sity's)
		$lists['sityid'] = JHTML::_('select.genericlist',   $sityl, 'sityid', array('onChange'=>'form.submit()'), $key = 'value', $text = 'text', $model->_sityid );

		// work_mode = 0 -> Link
		// work_mode = 1 -> Database
		switch($tmpl['work_mode']){
			case 0:
				$tpl = 'link';
				$item = $model->getDataLink();
				if ($model->_sityid == $model->_defsityid) {
					$stl = & $this->get('StationListWithLink');
					$selstation = JText::_( 'Select station' );
					array_unshift($stl, array("value"=>"0","text"=>"- $selstation -"));
					// build the html select list for metrostation
					$lists['stationid'] = JHTML::_('select.genericlist',   $stl, 'stationid', array('onChange'=>'document.location=this.value'), $key = 'value', $text = 'text', $model->_stationid );

					if (!empty($item['stations'])) {
						$javascript = "mapdata_stations=[\n";

						foreach ($item['stations'] as $st){
							list($x1,$y1,$x2,$y2) = explode(',',$st->mapcoords);
							$javascript .= '{id:\''.$st->stationlink.'\',name:\''.$st->stationname.'\',xs:'.$x1.',ys:'.$y1.'},'."\n";
						}

						$javascript .= "];\n";
						$javascript .= "var mode=0;\n";
						$document->addScriptDeclaration($javascript);
						$document->addScript( '/media/com_metromap/metromap_fns.js' );
					}
				}


				break;
			default:
				$item = $model->getData();
				if ($model->_sityid == $model->_defsityid) {
					$stl = & $this->get('ExistStationList');
					$selstation = JText::_( 'Select station' );
					array_unshift($stl, array("value"=>"0","text"=>"- $selstation -"));
					// build the html select list for metrostation
					$lists['stationid'] = JHTML::_('select.genericlist',   $stl, 'stationid', array('onChange'=>'form.submit()'), $key = 'value', $text = 'text', $model->_stationid );

					if (!empty($item['stations'])) {
						$javascript = "mapdata_stations=[\n";

						foreach ($item['stations'] as $st){
							list($x1,$y1,$x2,$y2) = explode(',',$st->mapcoords);
							$javascript .= '{id:\''.$st->id.'\',name:\''.$st->stationname.'\',xs:'.$x1.',ys:'.$y1.'},'."\n";
						}

						$javascript .= "];\n";
						$javascript .= "var mode=1;\n";
						$document->addScriptDeclaration($javascript);
						$document->addScript( '/media/com_metromap/metromap_fns.js' );
					}
				}
		} // switch

		$document->addStyleSheet('/media/com_metromap/metromap.css');
		$this->assignRef('tmpl', $tmpl );
		$this->assignRef('lists', $lists);
		$this->assignRef('defsityid', $model->_defsityid);
		$this->assignRef('sityid', $model->_sityid);
		$this->assignRef('stationid', $model->_stationid);
		$this->assignRef('item', $item);
		$this->assignRef('request_url',	$uri->toString());

		parent::display($tpl);
	}
}

?>